window.addEventListener('load', function () {
	const body = document.querySelector('body')
	const meun = document.querySelector('.menu')
	const menuBar = document.querySelector('.menu-bar')
	const flexMenuBtn = document.querySelector('.menu-btn')
	const search = document.querySelector('#default')
	const searchbar = document.querySelector('#default input')
	const onDark = document.querySelector('.on-dark')
	const spans = document.querySelectorAll('.menu li span')
	const menuBtn = document.querySelector('.menu-btn')
	const authorText = document.querySelector('.author-text')

	menuBtn.addEventListener('click', function () {
		meun.classList.toggle('shrink')
		this.children[0].classList.toggle('active-rotate')
		spans.forEach(item => {
			item.classList.toggle('hide')
		})
		authorText.classList.toggle('hide')
	})

	onDark.addEventListener('click', function () {
		this.classList.toggle('move')
		dark()
	})

	function dark() {
		body.classList.toggle('dark')
		meun.classList.toggle('menu-dark')
		menuBar.classList.toggle('hover-dark')
		search.classList.toggle('menu-bar-dark')
		searchbar.classList.toggle('menu-bar-dark')
		flexMenuBtn.classList.toggle('menu-bar-dark')
	}


	var the_logo_bg    = 'color_2',
        the_header_bg  = 'color_2',
        the_sidebar_bg = 'color_2';
	
	if (the_logo_bg) $('body').attr('data-logobg', the_logo_bg);
	if (the_header_bg) $('body').attr('data-headerbg', the_header_bg);
	if (the_sidebar_bg) $('body').attr('data-sidebarbg', the_sidebar_bg);
})

;jQuery( function() {

	console.log("index.js->jquery")
	 // 侧边栏
	 $(document).on('click', '.lsw-aside-toggler', function() {
        $('.lsw-layout-sidebar').toggleClass('lsw-aside-open');
        $("body").toggleClass('lsw-layout-sidebar-close');
        
        if ($('.lsw-mask-modal').length == 0) {
            $('<div class="lsw-mask-modal"></div>').prependTo('body');
        } else {
            $( '.lsw-mask-modal' ).remove();
        }
    });

	 // 遮罩层
	 $(document).on('click', '.lsw-mask-modal', function() {
        $( this ).remove();
    	$('.lsw-layout-sidebar').toggleClass('lsw-aside-open');
        $('body').toggleClass('lsw-layout-sidebar-close');
    });
    
	// 侧边栏导航
    $(document).on('click', '.nav-item-has-subnav > a', function() {
		$subnavToggle = jQuery( this );
		$navHasSubnav = $subnavToggle.parent();
        $topHasSubNav = $subnavToggle.parents('.nav-item-has-subnav').last();
		$subnav       = $navHasSubnav.find('.nav-subnav').first();
        $viSubHeight  = $navHasSubnav.siblings().find('.nav-subnav:visible').outerHeight();
        $scrollBox    = $('.lsw-layout-sidebar-info');
		$navHasSubnav.siblings().find('.nav-subnav:visible').slideUp(500).parent().removeClass('open');
		$subnav.slideToggle( 300, function() {
			$navHasSubnav.toggleClass( 'open' );
			
			// 新增滚动条处理
			var scrollHeight  = 0;
			    pervTotal     = $topHasSubNav.prevAll().length,
			    boxHeight     = $scrollBox.outerHeight(),
		        innerHeight   = $('.sidebar-main').outerHeight(),
                thisScroll    = $scrollBox.scrollTop(),
                thisSubHeight = $(this).outerHeight(),
                footHeight    = 121;
			
			if (footHeight + innerHeight - boxHeight >= (pervTotal * 48)) {
			    scrollHeight = pervTotal * 48;
			}
            if ($subnavToggle.parents('.nav-item-has-subnav').length == 1) {
                $scrollBox.animate({scrollTop: scrollHeight}, 300);
            } else {
                // 子菜单操作
                if (typeof($viSubHeight) != 'undefined' && $viSubHeight != null) {
                    scrollHeight = thisScroll + thisSubHeight - $viSubHeight;
                    $scrollBox.animate({scrollTop: scrollHeight}, 300);
                } else {
                    if ((thisScroll + boxHeight - $scrollBox[0].scrollHeight) == 0) {
                        scrollHeight = thisScroll - thisSubHeight;
                        $scrollBox.animate({scrollTop: scrollHeight}, 300);
                    }
                }
            }
		});
	});
})
